package com.logback.hello;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;

/**
 * Logback 入门示例
 *
 * @author innocent
 * @date 2018/12/29 17:00
 */
public class HelloLogback {

    /**
     * 成员变量初始化，需要先定义出变量名，后开发工具才能进行提示。
     *
     * @author ABC
     * @time 2018/12/29 17:10
     */
    private static final Logger log = LoggerFactory.getLogger(HelloLogback.class);

    @Test
    public void hello() {
        log.trace("trace...");
        log.debug("debug...");
        log.info("Hello  Logback ...");
        log.warn("warn...");
        log.error("error...");

        // 打印内部状态
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        StatusPrinter.print(loggerContext);

        // 获取root Logger
        Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        System.out.println("rootLogger = " + rootLogger);

        int i = 1;
        Student student = new Student("mike",17);
        // {} 占位符，提高格式化日志信息性能
        log.debug("这个日志的输出级别是 {},等级是 {}", student.toString(),i);
        log.debug("这个日志的输出级别是 {},等级是 {}" +  student.toString(),i);

    }

    @Data
    @AllArgsConstructor
    private final class Student{
        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

}
